package org.makcro.sms.services.chat.base.service;

import java.text.MessageFormat;

import org.apache.log4j.Logger;

import com.klikmee.sms.core.commons.filters.ProcessItemFilter;
import com.klikmee.sms.core.commons.queues.QueueItem;
import com.klikmee.sms.core.commons.queues.QueueItemProcessor;

public class DeliveryReportItemProcessor implements QueueItemProcessor<ResponseReport> {

	// Logger instance
	private Logger								logger	= Logger.getLogger( DeliveryReportItemProcessor.class );

	// chain of delivery report processor filters
	private ProcessItemFilter<ResponseReport>[]	responseReportFilters;

	@Override
	public boolean process(QueueItem<ResponseReport> arg0) {
		ResponseReport responseReport = arg0.getPayload();
		// assume that item is not processed
		boolean itemProcessed = false;

		// traverse filter chain until some filter gets activated
		for (ProcessItemFilter<ResponseReport> filter : responseReportFilters) {
			itemProcessed = filter.process( responseReport );
			if (itemProcessed) {
				// log it and break the loop
				logger.debug( MessageFormat.format( "Successfull filter activation on [{0}]", filter.toString() ) );
				break;
			}
		}
		
		return itemProcessed;
	}

	public void setResponseReportFilters(ProcessItemFilter<ResponseReport>[] responseReportFilters) {
		this.responseReportFilters = responseReportFilters;
	}
}
